<template>
  <div class="box" ref="chartDom"></div>
</template>
<script lang="ts" setup>
import * as echarts from 'echarts'
import { onMounted, ref } from 'vue'

const chartDom = ref<HTMLElement | null>(null)

const initChart = () => {
  const myChart = echarts.init(chartDom.value)
  const textColor = 'rgba(37, 76, 112, 1)'
  const xAxisData = ['四川', '重庆', '上海', '杭州', '北京']
  const seriesData = [10, 52, 200, 334, 390]
  const colorArr = [
    ['rgba(0, 147, 221, 1)', 'rgba(255, 255, 255, 1)'],
    ['rgba(0, 147, 221, 1)', 'rgba(0, 88, 255, 0.60)'],
    ['rgba(0, 147, 221, 1)', 'rgba(0, 88, 255, 0.40)'],
    ['rgba(10, 49, 108, 0.60)', 'rgba(27, 101, 155, 0.60)']
  ]
  window.addEventListener('resize', () => {
    myChart.resize()
  })
  const getRealData = (myChart, x, y, max) => {
    const pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, [x, y])
    if (pointInGrid[1] > max || pointInGrid[1] < 0) {
      return false
    } else {
      var xIndex = pointInGrid[0]
      return xAxisData[xIndex]
    }
  }
  //   根据点位查询在哪个柱子上
  const max = Math.max(...seriesData)
  myChart.getZr().on('click', (e) => {
    const result = getRealData(myChart, e.offsetX, e.offsetY, max)
    if (result) {
        console.log("在柱子上", result)
    }
  })

  const offsetX = 10 //bar宽
  const offsetY = 6 //倾斜角度
  // 绘制左侧面
  const CubeLeft = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0
    },
    buildPath: function (ctx:any, shape) {
      // 会canvas的应该都能看得懂，shape是从custom传入的
      const xAxisPoint = shape.xAxisPoint
      const c0 = [shape.x, shape.y]
      const c1 = [shape.x - offsetX, shape.y - offsetY]
      const c2 = [xAxisPoint[0] - offsetX, xAxisPoint[1] - offsetY]
      const c3 = [xAxisPoint[0], xAxisPoint[1]]
      ctx
        .moveTo(c0[0], c0[1])
        .lineTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .closePath()
    }
  })
  // 绘制右侧面
  const CubeRight = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0
    },
    buildPath: function (ctx:any, shape) {
      const xAxisPoint = shape.xAxisPoint
      const c1 = [shape.x, shape.y]
      const c2 = [xAxisPoint[0], xAxisPoint[1]]
      const c3 = [xAxisPoint[0] + offsetX, xAxisPoint[1] - offsetY]
      const c4 = [shape.x + offsetX, shape.y - offsetY]
      ctx
        .moveTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .lineTo(c4[0], c4[1])
        .closePath()
    }
  })
  // 绘制顶面
  const CubeTop = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0
    },
    buildPath: function (ctx:any, shape) {
      const c1 = [shape.x, shape.y]
      const c2 = [shape.x + offsetX, shape.y - offsetY] //右点
      const c3 = [shape.x, shape.y - offsetY * 2]
      const c4 = [shape.x - offsetX, shape.y - offsetY]
      ctx
        .moveTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .lineTo(c4[0], c4[1])
        .closePath()
    }
  })

  // 绘制底面
  const CubeBottom = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0
    },
    buildPath: function (ctx:any, shape) {
      const c1 = shape.xAxisPoint
      const c2 = [shape.x + offsetX, shape.xAxisPoint[1] - offsetY] //右点
      const c3 = [shape.x, shape.xAxisPoint[1] - offsetY * 2]
      const c4 = [shape.x - offsetX, shape.xAxisPoint[1] - offsetY]
      ctx
        .moveTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .lineTo(c4[0], c4[1])
        .closePath()
    }
  })
  // 注册三个面图形
  echarts.graphic.registerShape('CubeLeft', CubeLeft)
  echarts.graphic.registerShape('CubeRight', CubeRight)
  echarts.graphic.registerShape('CubeTop', CubeTop)
  echarts.graphic.registerShape('CubeBottom', CubeBottom)
  const option = {
    tooltip: {
      trigger: 'axis',
      enterable: true,
      axisPointer: {
        type: 'shadow',
        shadowStyle: {
          color: 'rgba(0, 0, 0, 0.1)'
        }
        // lineStyle: {
        //   color: "rgba(0, 0, 0, 0.1)",
        //   width: 60,
        //   type: "solid",
        // },
        // z: 0,
      },
      formatter: function (params) {
        const item = params[1]
        return item.name + ' : ' + item.value
      }
    },
    grid: {
      left: '5%',
      right: '5%',
      top: '18%',
      bottom: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: xAxisData,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        fontSize: 14,
        color: textColor
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      splitLine: {
        show: true,
        lineStyle: {
          width: 0.5,
          color: 'rgba(37, 76, 112, 1)'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        show: true,
        fontSize: 14,
        color: textColor
      }
    },
    series: [
      {
        type: 'custom',
        renderItem: (_, api) => {
          const location = api.coord([api.value(0), api.value(1)])
          return {
            type: 'group',
            children: [
              {
                type: 'CubeLeft',
                shape: {
                  api,
                  xValue: api.value(0),
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                  xAxisPoint: api.coord([api.value(0), 0])
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: colorArr[1][0]
                    },
                    {
                      offset: 1,
                      color: colorArr[1][1]
                    }
                  ])
                }
              },
              {
                type: 'CubeRight',
                shape: {
                  api,
                  xValue: api.value(0),
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                  xAxisPoint: api.coord([api.value(0), 0])
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: colorArr[2][0]
                    },
                    {
                      offset: 1,
                      color: colorArr[2][1]
                    }
                  ])
                }
              },
              {
                type: 'CubeTop',
                shape: {
                  api,
                  xValue: api.value(0),
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                  xAxisPoint: api.coord([api.value(0), 0])
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: colorArr[0][0]
                    },
                    {
                      offset: 0.5,
                      color: colorArr[0][0]
                    },
                    {
                      offset: 1,
                      color: colorArr[0][1]
                    }
                  ])
                }
              },
              {
                type: 'CubeBottom',
                shape: {
                  api,
                  xValue: api.value(0),
                  yValue: api.value(1),
                  x: location[0],
                  y: location[1],
                  xAxisPoint: api.coord([api.value(0), 0])
                },
                style: {
                  fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: colorArr[3][0]
                    },
                    {
                      offset: 1,
                      color: colorArr[3][1]
                    }
                  ])
                }
              }
            ]
          }
        },
        data: seriesData
      },
      {
        type: 'bar',
        itemStyle: {
          color: 'transparent'
        },
        tooltip: {},
        data: seriesData
      }
    ]
  }
  myChart.setOption(option)
}

onMounted(() => {
  initChart()
})
</script>
<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
}
</style>